''' Data collection functions.

A bunch of functions that take no parameters, and return a short string
containing interesting data about a specific statistic. Examples: uptime, disk
space, average load, whether Java is installed, top running processes...

This data will eventually be displayed in a big HTML table for all the nodes:
                space(bytes)     uptime(days)  ...
pluto.com       23231/43523                51   
mars.edu        43235/32423                27
venus.ca        32652/23652                63
...
'''

import commands

def hostname():   
    'Returns the machine name as given by bin/hostname'
    return commands.getoutput('hostname')

def disk_space():   
    'Returns the number of bytes used/available'
    disk = commands.getoutput('df -Ph')
    disk = disk.splitlines()
    index = 0
    for item in disk:
        disk[index] = item.split()
        index += 1
    return disk[1][2] + "/" + disk[1][1]

def uptime():
    'Returns the number of days the system has been running.'
    info = commands.getoutput('uptime')
    info = info.split()
    uptime = info[2]
    return uptime

def cpu_load():
    '''Returns 15-minute load average normalized for any number of cores.

Code based on this article:
http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages
    '''
    # find 15 minute load average by parsing uptime command
    uptime = commands.getoutput('uptime')
    # load average is at the end of the string, use reverse-split
    lavg = float(uptime.rsplit(None, 1)[-1])

    # find number of cores we're dealing with
    total_cores = num_cores()

    # return 15 minute load average, normalized by # of cores
    return str(lavg/total_cores)

    ## \/\/ OLD CODE \/\/
    ## Instantaneous load isn't much use to us. Let's see if this can be
    ## done better... :)  
    #'Returns the approximate instantaneous percentage load of CPU?'
    #info = commands.getoutput('ps -o pcpu')
    #info = info.split()
    #info.pop(0)
    #load = 0
    #for num in info:
        #           load += float(num)
    #return str(load)
    
def num_cores():
    'Returns the total number of cores'
    cores_info = commands.getoutput('grep "cpu cores" /proc/cpuinfo')
    total_cores = 0
    for line in cores_info.splitlines():
        # each line is for a separate CPU
        key,value = line.split(':')
        total_cores += int(value.strip())
    if total_cores ==0:
	total_cores = 1
    return total_cores

def num_cpus():
    'Returns the total number of processors'
    cpu_info = commands.getoutput('grep "processor" /proc/cpuinfo')
    cpu_info = cpu_info.rsplit(None, 1)
    return int(cpu_info[1]) + 1

def avg_cpu_freq():
    'Returns the average processor frequency in Mhz'
    freq_info = commands.getoutput('grep "cpu MHz" /proc/cpuinfo')
    freq_info = freq_info.splitlines()
    total_freq = 0
    for line in freq_info:
        # each line is for a separate CPU
        key,value = line.split(':')
        total_freq += float(value.strip())
    return int(total_freq / len(freq_info))

def boot_time():
    'returns the inital system boot time'
    # does not work on their machines - they don't have who
    boot_time = commands.getoutput('who -b')
    boot_time = boot_time.split()
    return boot_time[2]+" "+boot_time[3]
    
def memory():
    'Returns the number of MBs of memory used/available'
    mem_info = commands.getoutput('free -m')
    mem_info = mem_info.splitlines()[1].split()
    return mem_info[2] + "/" + mem_info[1]

def java():
    'Returns the Java version number if it has been installed'
    'or None if it has not been'
    #JAVA_PATH = '/home/usf_ubcslice5/jre1.6.0_16/bin/java' # CRIKEY
    java_info = commands.getoutput('java -version')
    java_info = java_info.splitlines()[0].split()
    if java_info[1] == "version":
        return java_info[2].strip('"')
    else:
        return None

def num_java_running():
    'Returns the number of currently running java processes.'
    return commands.getoutput('ps -A | grep java').count('java')
